<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增书籍')" />
	<th:block th:include="include :: select2-css" />
</head>
<body>
    <div class="main-content">
        <form id="form-book-add" class="form-horizontal">
            <input name="deptId" type="hidden" id="treeId"/>
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">书籍名称：</label>
                        <div class="col-sm-8">
                            <input name="bookName" id="bookName" placeholder="请输入书籍名称" class="form-control" type="text" maxlength="30" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属年级：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                            	<input name="treeName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择所在年级" class="form-control" required>
                                <span class="input-group-addon"><i class="fa fa-search"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">分类：</label>
                        <div class="col-sm-8">
                            <select id="bookMark" name="bookMark" class="form-control m-b">
                                <option value="科学类" selected>科学类</option>
                                <option value="文学类" >文学类</option>
                                <option value="童话书" >童话书</option>
                                <option value="漫画" >漫画</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">排序：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="orderNum" name="orderNum" class="form-control" type="text" maxlength="50" required>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">上传封面：</label>
                        <div class="col-sm-8">
                            <input id="file" name="file"  class="form-control" type="file" accept="image/*">
                        </div>
                    </div>
                </div>
            </div>

        </form>
    </div>
      
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: select2-js" />
	<script>
	    var prefix = ctx + "book";

        function submitHandler() {
	        if ($.validate.form()) {
	        	var formData = new FormData();
	        	formData.append('bookName', $("#bookName").val());
                formData.append('deptId', $("#treeId").val());
                formData.append('bookMark', $("#bookMark").val());
                formData.append('orderNum', $("#orderNum").val());
                //添加文件
                formData.append('file', $('#file')[0].files[0]);
                $.ajax({
                    url: prefix + "/addBook",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
	        }
	    }
         
        /* 用户管理-新增-选择班级树 */
        function selectDeptTree() {
        	var treeId = $("#treeId").val();
        	var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        	var url = ctx + "book/selectDeptTree/" + deptId;
			var options = {
				title: '选择班级',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			var body = layer.getChildFrame('body', index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			layer.close(index);
		}

		$(function() {
            $('#post').select2({
                placeholder: "请选择岗位",
                allowClear: true
            });
        })
    </script>
</body>
</html>